package controller

import (
	"danmu/common"
	"danmu/dto"
	"danmu/model"
	"danmu/response"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetDanmu(ctx *gin.Context)  {
	DB :=common.GetDB()
	var danmuList []model.Danmu
	vid := ctx.DefaultQuery("vid", "0")
	if vid == "0" {
		response.Fail(ctx,nil,"视频不存在")
		return
	}
	DB.Where("vid = ? ", vid).Find(&danmuList)
	ctx.JSON(http.StatusOK,gin.H{"code":200,"data":dto.ToDanmuDto(danmuList),"msg":"获取成功"})
}

func SendDanmu(ctx *gin.Context){
	DB :=common.GetDB()
	var requestDanmu = model.Danmu{}
	err := ctx.ShouldBind(&requestDanmu)
	if err != nil{
		response.Response(ctx,http.StatusBadRequest,400,nil,"请求错误")
		return
	}
	//内容
	vid := requestDanmu.Vid
	text := requestDanmu.Text
	time := requestDanmu.Time
	color :=requestDanmu.Color
	typeString := requestDanmu.Type
	id,_ :=ctx.Get("id")
	newDanmu := model.Danmu{
		Vid:vid,
		Uid:id.(uint),
		Text:text,
		Time:time,
		Color:color,
		Type:typeString,
	}
	DB.Create(&newDanmu)
	response.Success(ctx,nil,"发送成功")
}
